import base64
import os

from flask import request

from DBMoodls.User import User
from routes.app import db
from datetime import datetime
from DBMoodls.Working import Working
from DBMoodls.Image import Image



class WorkingService:

    #将修饰后的图片路径存入数据库
    def add_working(self, uid, size, pathOld,xmin, ymin, xmax, ymax,classification,pathNew):
        """
        :param uid:  图片所属用户id
        :param size: 图片大小
        :param path: 图片本地路径
        :return:
        """
        createdat = self.get_current_time()
        working = Working(uid=uid, size=size, createdat=createdat, pathOld=pathOld,xmin=xmin, ymin=ymin, xmax=xmax, ymax=ymax,classification=classification,pathNew=pathNew)
        db.session.add(working)
        db.session.commit()
        return working
#
#处理用户上传的图片
    def add_DB_image(self, uid, path,filename):
        """
        :param uid:  图片所属用户id
        :param path: 图片本地路径
        :param filename: 图片名
        :return:
        """
        created_at = self.get_current_time()
        image = Image(uid=uid, created_at=created_at, path=path,filename=filename)
        db.session.add(image)
        db.session.commit()
        return image
    #获取当前时间
    def del_DB_image(self, id):
        """
        :param id: 图片id
        :return:
        """
        image = db.session.query(Image).filter_by(id=id).first()
        db.session.delete(image)
        db.session.commit()
        return image
    # 根据用户uid在数据库中查询用户相关的所有图片路径
    def get_DB_image(self, uid):
        """
        :param uid: 用户id
        :return: 包含图片属性的字典列表
        """
        images = db.session.query(Image).filter_by(uid=uid).all()
        image_dicts = []

        for image in images:
            abs_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), image.path)
            with open(abs_path, 'rb') as img_file:
                encoded_string = base64.b64encode(img_file.read()).decode('utf-8')
            image_dicts.append({
                'id': image.id,
                'uid': image.uid,
                'path': encoded_string,
                'created_at': image.created_at,
                'filename': image.filename
            })
        print(image_dicts)
        return image_dicts


    def get_current_time(self):
        """
        获取当前时间
        :return:
        """
        return datetime.now()

    # 处理用户处理完保存后的图片
    # 查询用户所有图片
    def get_working(self, uid):
        """
        :param uid: 用户id
        :return: 包含图片属性的字典列表
        """
        workings = db.session.query(Working).filter_by(uid=uid).all()
        working_dicts = []
        for working in workings:
            abs_path_old = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), working.pathOld)
            with open(abs_path_old, 'rb') as img_file:
                encoded_string_old = base64.b64encode(img_file.read()).decode('utf-8')
            abs_path_new =os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), working.pathNew)
            with open(abs_path_new, 'rb') as img_file:
                encoded_string_new = base64.b64encode(img_file.read()).decode('utf-8')
            working_dicts.append({
                'uid': working.uid,
                'id': working.id,
                'size': working.size,
                'created_at': working.createdat,
                'pathOld': encoded_string_old,
                'xmin': working.xmin,
                'ymin': working.ymin,
                'xmax': working.xmax,
                'ymax': working.ymax,
                'classification': working.classification,
                'pathNew': encoded_string_new
            })
        print(working_dicts)
        return working_dicts
    #根据前端传来的图片id序列来在数据库中查询需要导出yolo格式的的workings序列
    def get_working_toFile(self,id_list):
        """
        :param id_list: 图片id列表
        :return: 包含图片属性的字典列表
        """
        #id_list = request.json.get('id_list')
        workings = db.session.query(Working).filter(Working.id.in_(id_list)).all()
        working_dicts = []
        for working in workings:
            working_dicts.append({
                'uid': working.uid,
                'id': working.id,
                'size': working.size,
                'created_at': working.createdat,
                'pathOld': working.pathOld,
                'xmin': working.xmin,
                'ymin': working.ymin,
                'xmax': working.xmax,
                'ymax': working.ymax,
                'classification': working.classification,
                'pathNew': working.pathNew
            })
        print(working_dicts)
        return working_dicts
    #删除图片
    def delete_working(self, id):
        """
        :param id: 图片id
        :return:
        """
        working = db.session.query(Working).filter_by(id=id).first()
        db.session.delete(working)
        db.session.commit()
        return working
